package advanced.network.basic_servlet;

import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;

public class FrameTest {
    public static void main(String[] args) {
        //创建xml解析工厂对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            SAXParser saxParser = factory.newSAXParser();
            File xml = new File(Thread.currentThread().getContextClassLoader().getResource("advanced/network/basic_servlet/web.xml").toURI());
            //创建自定义的处理器
            WebHandler webHandler = new WebHandler();
            saxParser.parse(xml, webHandler);

            WebContext context = new WebContext(webHandler.getEntitis(), webHandler.getMappings());


            //模仿浏览器请求了/reg这个action
            String claName = context.getClz("/login");
            Class clz = Class.forName(claName);
            Servlet servlet = (Servlet) clz.getConstructor().newInstance();
            //            servlet.service();

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            System.out.println("找不到该类");
            System.out.println("**.**.servlet 不是/ 的路径");
            e.printStackTrace();
        } catch (InstantiationException e) {
            System.out.println("反射异常");
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
